/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client.gui.smilies;

import client.communication.ClientCommunication;
import java.util.ResourceBundle;
import javax.swing.ImageIcon;
import javax.swing.text.BadLocationException;
import javax.swing.text.Element;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import org.apache.log4j.Logger;

/**
 *
 * @author Liesbeth
 */
public class SmileyVerwerker {

    private static Logger logger = Logger.getLogger(SmileyVerwerker.class);

    private SmileyVerwerker() {

    }

    public static void parseSmileys(String path, StyledDocument doc, String text, int lengthAddedText) throws BadLocationException {
        smileysMetDubbelePunt(path, doc, text, lengthAddedText);
        smileysMetPuntKomma(path, doc, text, lengthAddedText);
        smileysMetSlashForward(path, doc, text, lengthAddedText);
        smileysMetHoedje(path, doc, text, lengthAddedText);
        smileysMetHaakje(path, doc, text, lengthAddedText);
    }

    private static void smileysMetDubbelePunt(String path, StyledDocument doc, String text, int lengthAddedText) throws BadLocationException {
        SimpleAttributeSet smi;
        int index;
        int start = 0;
        char c;
        index = text.indexOf(':', text.length() - lengthAddedText);
        while (index > -1 && index + 1 < text.length()) {

            Element e1 = doc.getCharacterElement(index);
            String s = ":";
            if (StyleConstants.getIcon(e1.getAttributes()) == null) {
                c = text.charAt(index + 1);
                boolean def = true;
                switch (c) {
                    case 'D':
                        s += "D";
                        doc.remove(index, 2);
                        smi = new SimpleAttributeSet();
                        StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                        doc.insertString(index, s, smi);
                        start = (index + 2);
                        break;
                    case '(':
                        s += "(";
                        doc.remove(index, 2);
                        smi = new SimpleAttributeSet();
                        StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                        doc.insertString(index, s, smi);
                        start = (index + 2);
                        break;
                    case 'p':
                        s += "p";
                        if (index + 3 <= text.length()) {
                            c = text.charAt(index + 2);
                            switch (c) {
                                case 'p':
                                    def = false;
                                    s += "p";
                                    doc.remove(index, 3);
                                    smi = new SimpleAttributeSet();
                                    StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                                    doc.insertString(index, s, smi);
                                    start = (index + 3);
                                    break;
                                default:
                                    def = true;
                            }
                        }
                        if (def) {
                            doc.remove(index, 2);
                            smi = new SimpleAttributeSet();
                            StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                            doc.insertString(index, s, smi);
                            start = (index + 2);
                        }
                        break;
                    case '@':
                        s += "@";
                        doc.remove(index, 2);
                        smi = new SimpleAttributeSet();
                        StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                        doc.insertString(index, s, smi);
                        start = (index + 2);
                        break;
                    case 's':
                        s += "s";
                        if (index + 3 <= text.length()) {
                            c = text.charAt(index + 2);
                            switch (c) {
                                case '?':
                                    def = false;
                                    s += "?";
                                    doc.remove(index, 3);
                                    smi = new SimpleAttributeSet();
                                    StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                                    doc.insertString(index, s, smi);
                                    start = (index + 3);
                                    break;
                                default:
                                    def = true;
                            }
                        }
                        if (def) {
                            doc.remove(index, 2);
                            smi = new SimpleAttributeSet();
                            StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                            doc.insertString(index, s, smi);
                            start = (index + 2);
                        }
                        break;
                    case '8':
                        s += "8";
                        doc.remove(index, 2);
                        smi = new SimpleAttributeSet();
                        StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                        doc.insertString(index, s, smi);
                        start = (index + 2);
                        break;
                    case ')':
                        s += ")";
                        doc.remove(index, 2);
                        smi = new SimpleAttributeSet();
                        StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                        doc.insertString(index, s, smi);
                        start = (index + 2);
                        break;
                    case 'o':
                        s += "o";
                        doc.remove(index, 2);
                        smi = new SimpleAttributeSet();
                        StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                        doc.insertString(index, s, smi);
                        start = (index + 2);
                        break;
                    default:
                        start = (index + 1);
                }
            } else {
                start = (index + 1);
            }
            index = (text.indexOf(':', start));
        }
    }

    private static void smileysMetPuntKomma(String path, StyledDocument doc, String text, int lengthAddedText) throws BadLocationException {
        SimpleAttributeSet smi;
        int index;
        int start = 0;
        char c;
        index = text.indexOf(';', text.length() - lengthAddedText);
        while (index > -1 && index + 1 < text.length()) {

            Element e1 = doc.getCharacterElement(index);
            String s = ";";
            if (StyleConstants.getIcon(e1.getAttributes()) == null) {
                c = text.charAt(index + 1);
                boolean def = true;
                switch (c) {
                    case ')':
                        s += ")";
                        doc.remove(index, 2);
                        smi = new SimpleAttributeSet();
                        StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                        doc.insertString(index, s, smi);
                        start = (index + 2);
                        break;
                    default:
                        start = (index + 1);
                }
            } else {
                start = (index + 1);
            }
            index = (text.indexOf(';', start));
        }
    }

    private static void smileysMetHoedje(String path, StyledDocument doc, String text, int lengthAddedText) throws BadLocationException {
        SimpleAttributeSet smi;
        int index;
        int start = 0;
        char c;
        index = text.indexOf('^', text.length() - lengthAddedText);
        while (index > -1 && index + 1 < text.length()) {

            Element e1 = doc.getCharacterElement(index);
            String s = "^";
            if (StyleConstants.getIcon(e1.getAttributes()) == null) {
                c = text.charAt(index + 1);
                switch (c) {
                    case 'o':
                        s += "o";
                        if (index + 3 <= text.length()) {
                            c = text.charAt(index + 2);
                            switch (c) {
                                case ')':
                                    s += ")";
                                    doc.remove(index, 3);
                                    smi = new SimpleAttributeSet();
                                    StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                                    doc.insertString(index, s, smi);
                                    start = (index + 3);
                                    break;
                                default:
                                    start = (index + 1);
                            }
                        } else {
                            start = (index + 1);
                        }
                        break;
                    default:
                        start = (index + 1);
                }
            } else {
                start = (index + 1);
            }
            index = (text.indexOf('^', start));
        }
    }

    private static void smileysMetHaakje(String path, StyledDocument doc, String text, int lengthAddedText) throws BadLocationException {
        SimpleAttributeSet smi;
        int index;
        int start = 0;
        char c;
        index = text.indexOf('(', text.length() - lengthAddedText);
        while (index > -1 && index + 1 < text.length()) {

            Element e1 = doc.getCharacterElement(index);
            String s = "(";
            if (StyleConstants.getIcon(e1.getAttributes()) == null) {
                c = text.charAt(index + 1);
                switch (c) {
                    case 'a':
                        s += "a";
                        if (index + 3 <= text.length()) {
                            c = text.charAt(index + 2);
                            switch (c) {
                                case ')':
                                    s += ")";
                                    doc.remove(index, 3);
                                    smi = new SimpleAttributeSet();
                                    StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                                    doc.insertString(index, s, smi);
                                    start = (index + 3);
                                    break;
                                default:
                                    start = (index + 1);
                            }
                        } else {
                            start = (index + 1);
                        }
                        break;
                    default:
                        start = (index + 1);
                }
            } else {
                start = (index + 1);
            }
            index = (text.indexOf('(', start));
        }
    }

    private static void smileysMetSlashForward(String path, StyledDocument doc, String text, int lengthAddedText) throws BadLocationException {
        SimpleAttributeSet smi;
        int index;
        int start = 0;
        char c;
        index = text.indexOf('/', text.length() - lengthAddedText);
        while (index > -1 && index + 1 < text.length()) {

            Element e1 = doc.getCharacterElement(index);
            String s = "/";
            if (StyleConstants.getIcon(e1.getAttributes()) == null) {
                c = text.charAt(index + 1);
                boolean def = true;
                switch (c) {
                    case 'b':
                        s += "b";
                        doc.remove(index, 2);
                        smi = new SimpleAttributeSet();
                        StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                        doc.insertString(index, s, smi);
                        start = (index + 2);
                        break;
                    case 'c':
                        s += "c";
                        if (index + 3 <= text.length()) {
                            c = text.charAt(index + 2);
                            switch (c) {
                                case 'r':
                                    def = false;
                                    s += "r";
                                    doc.remove(index, 3);
                                    smi = new SimpleAttributeSet();
                                    StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                                    doc.insertString(index, s, smi);
                                    start = (index + 3);
                                    break;
                                case 'g':
                                    def = false;
                                    s += "g";
                                    doc.remove(index, 3);
                                    smi = new SimpleAttributeSet();
                                    StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                                    doc.insertString(index, s, smi);
                                    start = (index + 3);
                                    break;
                                default:
                                    def = true;
                            }
                        }
                        if (def) {
                            doc.remove(index, 2);
                            smi = new SimpleAttributeSet();
                            StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                            doc.insertString(index, s, smi);
                            start = (index + 2);
                        }
                        break;
                    case 'h':
                        s += "h";
                        if (index + 3 <= text.length()) {
                            c = text.charAt(index + 2);
                            switch (c) {
                                case 'a':
                                    def = false;
                                    s += "a";
                                    doc.remove(index, 3);
                                    smi = new SimpleAttributeSet();
                                    StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                                    doc.insertString(index, s, smi);
                                    start = (index + 3);
                                    break;
                                default:
                                    def = true;
                            }
                        }
                        if (def) {
                            doc.remove(index, 2);
                            smi = new SimpleAttributeSet();
                            StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                            doc.insertString(index, s, smi);
                            start = (index + 2);
                        }
                        break;
                    case 'e':
                        s += "e";
                        if (index + 3 <= text.length()) {
                            c = text.charAt(index + 2);
                            switch (c) {
                                case 's':
                                    def = false;
                                    s += "s";
                                    doc.remove(index, 3);
                                    smi = new SimpleAttributeSet();
                                    StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                                    doc.insertString(index, s, smi);
                                    start = (index + 3);
                                    break;
                                case 'l':
                                    def = false;
                                    s += "l";
                                    doc.remove(index, 3);
                                    smi = new SimpleAttributeSet();
                                    StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                                    doc.insertString(index, s, smi);
                                    start = (index + 3);
                                    break;
                                default:
                                    def = true;
                            }
                        }
                        if (def) {
                            doc.remove(index, 2);
                            smi = new SimpleAttributeSet();
                            StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                            doc.insertString(index, s, smi);
                            start = (index + 2);
                        }
                        break;
                    case 'i':
                        s += "i";
                        doc.remove(index, 2);
                        smi = new SimpleAttributeSet();
                        StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                        doc.insertString(index, s, smi);
                        start = (index + 2);
                        break;
                    case 'm':
                        s += "m";
                        doc.remove(index, 2);
                        smi = new SimpleAttributeSet();
                        StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                        doc.insertString(index, s, smi);
                        start = (index + 2);
                        break;
                    case 'p':
                        s += "p";
                        if (index + 3 <= text.length()) {
                            c = text.charAt(index + 2);
                            switch (c) {
                                case 's':
                                    def = false;
                                    s += "s";
                                    doc.remove(index, 3);
                                    smi = new SimpleAttributeSet();
                                    StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                                    doc.insertString(index, s, smi);
                                    start = (index + 3);
                                    break;
                                default:
                                    def = true;
                            }
                        }
                        if (def) {
                            doc.remove(index, 2);
                            smi = new SimpleAttributeSet();
                            StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                            doc.insertString(index, s, smi);
                            start = (index + 2);
                        }
                        break;
                    case 'r':
                        s += "r";
                        if (index + 3 <= text.length()) {
                            c = text.charAt(index + 2);
                            switch (c) {
                                case 'a':
                                    s += "a";
                                    doc.remove(index, 3);
                                    smi = new SimpleAttributeSet();
                                    StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                                    doc.insertString(index, s, smi);
                                    start = (index + 3);
                                    break;
                                default:
                                    start = (index + 1);
                            }
                        } else {
                            start = (index + 1);
                        }
                        break;
                    case 's':
                        s += "s";
                        if (index + 3 <= text.length()) {
                            c = text.charAt(index + 2);
                            switch (c) {
                                case 'p':
                                    def = false;
                                    s += "p";
                                    doc.remove(index, 3);
                                    smi = new SimpleAttributeSet();
                                    StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                                    doc.insertString(index, s, smi);
                                    start = (index + 3);
                                    break;
                                case 'h':
                                    def = false;
                                    s += "h";
                                    doc.remove(index, 3);
                                    smi = new SimpleAttributeSet();
                                    StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                                    doc.insertString(index, s, smi);
                                    start = (index + 3);
                                    break;
                                default:
                                    def = true;
                            }
                        }
                        if (def) {
                            doc.remove(index, 2);
                            smi = new SimpleAttributeSet();
                            StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                            doc.insertString(index, s, smi);
                            start = (index + 2);
                        }
                        break;
                    case 't':
                        s += "t";
                        if (index + 3 <= text.length()) {
                            c = text.charAt(index + 2);
                            switch (c) {
                                case 'l':
                                    s += "l";
                                    doc.remove(index, 3);
                                    smi = new SimpleAttributeSet();
                                    StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                                    doc.insertString(index, s, smi);
                                    start = (index + 3);
                                    break;
                                default:
                                    start = (index + 1);
                            }
                        } else {
                            start = (index + 1);
                        }
                        break;
                    case 'z':
                        s += "z";
                        doc.remove(index, 2);
                        smi = new SimpleAttributeSet();
                        StyleConstants.setIcon(smi, new ImageIcon(SmileyVerwerker.class.getResource(path + ClientCommunication.getInstance().getSmilies().get(s))));
                        doc.insertString(index, s, smi);
                        start = (index + 2);
                        break;
                    default:
                        start = (index + 1);
                }
            } else {
                start = (index + 1);
            }
            index = (text.indexOf('/', start));
        }
    }
}
